<?php
namespace app\daili\controller;
use think\facade\Db;
use think\facade\Request;

class Daili extends  Base
{


    //友情链接列表
    public function index()
    {
      $loginAdmin=session('dailiAccount'); 
        $daili=Db::name('daili')->where('id',$loginAdmin['id'])->find();
      if($daili['status']==1){
            return alert('您代理等级不足，请联系上级代理升级!','/daili/index',5,3); 
            exit;
        }
   	$dailiData=Db::name('dailiconfig')->order('id desc')->select();
      return view('',[
         'dailiData'=>$dailiData,
        
        ]);

    }
     public function indexedit()
     {
    $id=input('id');
        $edir=Db::name('dailiconfig')->where('id',$id)->find();
       if(request()->isPost()){
         $data=request()->post();
     
        
       
         $res=Db::name('dailiconfig')->where('id',$id)->update($data);
         if($res){
            return alert('操作成功！','index',6,3);
         }else{
            return alert('操作失败！','index',5,3);
         }
       }
       return view('',[
       'edir'=>$edir,
        ]);
    }
     public function add(){
      $loginAdmin=session('dailiAccount'); 
       $mydata=Db::name('daili')->where('id',$loginAdmin['id'])->find();
      
       if(request()->isPost()){
         $data=request()->post();
         $data['time']=time();
         $data['userpass']=$this->password_salt($data['userpass']);
         $data['topid']=$loginAdmin['id'];
         $res=Db::name('daili')->insertGetId($data);
         if($res){
             
        $datas=Db::name('danqun')->where('id',1)->find();
        unset($datas['id']);
        $datas['uid']=$res;
        $res1=Db::name('danqun')->insertGetId($datas); 
         $paid=Db::name('paid')->where('id',1)->find();
         $paid['uid']=$res;
         unset($paid['id']);
         $paids=Db::name('paid')->insert($paid);
       $danqunlist=Db::name('danqunlist')->where('uid',0)->select()->toArray(); 
       foreach ($danqunlist as $k1 => $v1){
      
        unset($danqunlist[$k1]["id"]);
        $danqunlist[$k1]["uid"]=$res;
      
         }
           $danqunlists=Db::name('danqunlist')->insertAll($danqunlist);
             
            return alert('操作成功！','list',6,3);
         }else{
            return alert('操作失败！','list',5,3);
         }
       }
       return view('',[
       'mydata'=>$mydata,
        ]);
    }
  public function edit(){
       $id=input('id');
        $edir=Db::name('daili')->where('id',$id)->find();
       if(request()->isPost()){
         $data=request()->post();
         $data['time']=time();
         if($edir['userpass']!==$data['userpass']){
             $data['userpass']=$this->password_salt($data['userpass']);    
         }
        
       
         $res=Db::name('daili')->where('id',$id)->update($data);
         if($res){
            return alert('操作成功！','list',6,3);
         }else{
            return alert('操作失败！','list',5,3);
         }
       }
       return view('',[
       'edir'=>$edir,
        ]);
    }
public function list()
    {
         $loginAdmin=session('dailiAccount'); 
        $daili=Db::name('daili')->where('id',$loginAdmin['id'])->find();
        if($daili['status']==1){
            return alert('您代理等级不足，请联系上级代理升级!','/daili/index',5,3); 
            exit;
        }
    	$dailiData=Db::name('daili')->where('topid',$loginAdmin['id'])->order('id desc')->select();
      return view('',[
         'dailiData'=>$dailiData,
         'daili'=>$daili,
        
        ]);

    }
    /*添加*/
  public function del()
    {
       $id=input('id');//助手函数

      $res=Db::name('daili')->delete($id);
      $res1=Db::name('paid')->where('uid',$id)->delete();
      $res2=Db::name('danqunlist')->where('uid',$id)->delete();
      $res3=Db::name('danqun')->where('uid',$id)->delete();
      if($res){
            return alert('删除成功!','list',6,3);
         }else{
            return alert('操作失败！','list',5,3);
         } 
    }   
    
    
}
